package top.yxf.gulimall.coupon.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import top.yxf.common.to.MemberPrice;
import top.yxf.common.to.SkuReductionTo;
import top.yxf.common.utils.PageUtils;
import top.yxf.common.utils.Query;

import top.yxf.gulimall.coupon.dao.SkuFullReductionDao;
import top.yxf.gulimall.coupon.entity.MemberPriceEntity;
import top.yxf.gulimall.coupon.entity.SkuFullReductionEntity;
import top.yxf.gulimall.coupon.entity.SkuLadderEntity;
import top.yxf.gulimall.coupon.service.MemberPriceService;
import top.yxf.gulimall.coupon.service.SkuFullReductionService;
import top.yxf.gulimall.coupon.service.SkuLadderService;

import javax.annotation.Resource;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {


    @Resource
    private SkuLadderService  skuLadderService;

    @Resource
    private MemberPriceService memberPriceService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuReduction(SkuReductionTo skuFullReduction) {

        // sku的优惠满减信息 gulimall_sms库 -》 sms_sku_full->sms_sku_ladder ->  sms_sku_full_reduction -> sms_member_price
        // 保存满减打折会员价 sms_sku_ladder
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        skuLadderEntity.setSkuId(skuFullReduction.getSkuId());
        skuLadderEntity.setFullCount(skuFullReduction.getFullCount());
        skuLadderEntity.setDiscount(skuFullReduction.getDiscount());
        skuLadderEntity.setAddOther(skuFullReduction.getCountStatus());

        if (skuFullReduction.getFullCount() > 0) {
            skuLadderService.save(skuLadderEntity);
        }



        //  sms_sku_full_reduction
        SkuFullReductionEntity entity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuFullReduction, entity);

        if (skuFullReduction.getFullPrice().compareTo(new BigDecimal("0")) == 1) {
            this.save(entity);
        }

        // sms_member_price

        List<MemberPrice> memberPrice = skuFullReduction.getMemberPrice();

        List<MemberPriceEntity> priceEntityList = memberPrice.stream().map(member -> {
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            memberPriceEntity.setSkuId(skuFullReduction.getSkuId());
            memberPriceEntity.setMemberLevelName(member.getName());
            memberPriceEntity.setMemberLevelId(member.getId());
            memberPriceEntity.setMemberPrice(member.getPrice());
            memberPriceEntity.setAddOther(1);
            return memberPriceEntity;
        }).filter(item -> item.getMemberPrice().compareTo(new BigDecimal("0")) == 1).collect(Collectors.toList());
        memberPriceService.saveBatch(priceEntityList);

    }

}